﻿package cn.smbms.service.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.user.UserDao;
import cn.smbms.pojo.User;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */

@Service("UserService")
public class UserServiceImpl implements UserService{
	@Resource(name="userDao")
	private UserDao userDao1;
	
	
	@Transactional
	public boolean add(User user) {
		// TODO Auto-generated method stub
	boolean flag=false;
			int updateRows = userDao1.add(user);
			
			if(updateRows > 0){
				flag = true;
				System.out.println("add success!");
			}else{
				System.out.println("add failed!");
			}
		return flag;
	}
	
	public User login(User user) {
		// TODO Auto-generated method stub
		User u = userDao1.login(user);
		
		return u;
	}
	
	public List<User> getUserList(String queryUserName,Integer queryUserRole,int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		int start=(currentPageNo-1)*pageSize;
		User u=new User();
		u.setUserName(queryUserName);
		u.setUserRole(queryUserRole);
		u.setStart(start);
		u.setPageSize(pageSize);
		List<User> userList=null;
		userList = userDao1.getUserList(u);
		
		
		return userList;
	}
	
	public User selectUserCodeExist(String userCode) {
		// TODO Auto-generated method stub
	
		User user = null;
		try {
			
			user = userDao1.getUser(userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	
	public boolean deleteUserById(Integer delId) {
		// TODO Auto-generated method stub
	
		boolean flag = false;
		try {
			
			if(userDao1.deleteUserById(delId) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	public User getUserById(Integer id) {
		// TODO Auto-generated method stub
		User user = null;
		
		try{
			
			user = userDao1.getUserById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			user = null;
		}
		return user;
	}
	
	public boolean modify(User user) {
		// TODO Auto-generated method stub
		
		boolean flag = false;
		try {
		
			if(userDao1.modify(user) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	
	public boolean updatePwd(int id, String pwd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		
		try{
			
			if(userDao1.updatePwd(id,pwd) > 0)
				flag = true;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
	
	public int getUserCount(String queryUserName, Integer queryUserRole) {
		// TODO Auto-generated method stub
		User u=new User();
		u.setUserName(queryUserName);
		u.setUserRole(queryUserRole);
		int count = userDao1.getUserCount(u);
		
		return count;
	}

	public List<User> getUsers(int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		return userDao1.getUsers(currentPageNo, pageSize);
	}
	
	
}
